package com.zcc.makingfriend.controller;

import com.zcc.makingfriend.dao.Article;
import com.zcc.makingfriend.dto.ArticleCategoryDto;
import com.zcc.makingfriend.dto.ArticleImageDto;
import com.zcc.makingfriend.dto.PageDto;
import com.zcc.makingfriend.service.ArticleService;
import com.zcc.makingfriend.untils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author zcc
 */
@Api(tags = "文章管理")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @ApiOperation("查询所有文章")
    @GetMapping("/getAll/{userId}/{page}/{size}")
    public Result getAllArticles(@PathVariable String userId, @PathVariable int page, @PathVariable int size){
        List<ArticleImageDto> allArticles = articleService.getAllArticles(userId, (page - 1) * size, size);
        return Result.success(allArticles);
    }

    @ApiOperation("通过分类查询所有文章")
    @GetMapping("/getAllByCategory/{title}/{userId}/{page}/{size}")
    public Result getAllArticlesByCategory(@PathVariable String title, @PathVariable String userId, @PathVariable int page, @PathVariable int size){
        List<ArticleImageDto> allArticles = articleService.getAllArticlesByCategory(title, userId, (page - 1) * size, size);
        return Result.success(allArticles);
    }

    @ApiOperation("通过用户查询关注者文章")
    @GetMapping("/getAllByUser/{userId}")
    public Result getAllArticlesByUser(@PathVariable String userId){
        List<ArticleImageDto> allArticles = articleService.getAllArticlesByUser(userId);
        return Result.success(allArticles);
    }

    @ApiOperation("模糊搜索已经存在的文章")
    @GetMapping("/searchArticle/{content}/{userId}/{page}/{size}")
    public Result searchArticle(@PathVariable String content, @PathVariable String userId, @PathVariable int page, @PathVariable int size){
        List<ArticleImageDto> allArticles = articleService.searchArticle(content, userId, (page - 1) * size, size);
        return Result.success(allArticles);
    }

    @ApiOperation("查询喜欢的文章")
    @GetMapping("/getlikes/{userId}/{page}/{size}")
    public Result getLikeArticles(@PathVariable String userId, @PathVariable int page, @PathVariable int size){
        List<ArticleImageDto> allArticles = articleService.getLikeArticles(userId, (page - 1) * size, size);
        return Result.success(allArticles);
    }

    @ApiOperation("统计用户喜欢的文章数量")
    @GetMapping("/countLikes/{userId}")
    public Result countLikes(@PathVariable String userId){
        Long aLong = articleService.countUserLiked(userId);
        return Result.success(aLong);
    }

    @ApiOperation("查询用户自己的文章")
    @GetMapping("/getArticleByAuthor/{authorId}/{page}/{size}")
    public Result getArticleByAuthor(@PathVariable Long authorId, @PathVariable int page, @PathVariable int size){
        List<ArticleImageDto> allArticles = articleService.getArticleByAuthor(authorId, (page - 1) * size, size);
        return Result.success(allArticles);
    }

    @ApiOperation("模糊查询文章")
    @PostMapping("/byCondition")
    public Result getArticleByCondition(@RequestBody PageDto pageDto) {
        List<ArticleImageDto> articlesByCondition = articleService.getArticlesByCondition(pageDto);
        return Result.success(articlesByCondition);
    }

    @ApiOperation("新增文章")
    @PostMapping("/add")
    public Result addArticle(@RequestBody ArticleCategoryDto article) {
        System.out.println(article);
        String i = articleService.addArticle(article);
        if (i == null) {
            return Result.fail("新增文章失败");
        } else if ("0".equals(i)) {
            return Result.fail("用户已被冻结，请联系管理员");
        } else {
            return Result.success(i);
        }
    }

    @ApiOperation("删除文章")
    @PutMapping("/delete/{uuid}")
    public Result deleteArticle(@PathVariable String uuid) {
        int i = articleService.deleteArticle(uuid);
        if (i == 0) {
            return Result.success("删除文章成功");
        }
        return Result.fail("删除文章失败");
    }


    @ApiOperation("更新文章")
    @PostMapping("/update")
    public Result updateArticle(@RequestBody ArticleCategoryDto article) {
        String i = articleService.updateArticle(article);
        if (!i.isEmpty()) {
            return Result.success(i);
        }
        return Result.fail("修改文章失败");
    }

    @ApiOperation("点赞/取消点赞文章")
    @PutMapping("/isLike/{status}/{likes}/{uuid}")
    public Result isLikeArticle(@PathVariable int status, @PathVariable int likes, @PathVariable String uuid) {
        int likeArticle = articleService.isLikeArticle(status, likes, uuid);
        if (likeArticle == likes) {
            return Result.fail("操作失败");
        }
        return Result.success(likeArticle);
    }
    @ApiOperation("通过uuid查找文章")
    @GetMapping("/getArticleByUuid/{uuid}")
    public Result getArticleByUuid(@PathVariable String uuid) {
        ArticleImageDto articleByUUid = articleService.getArticleByUUid(uuid);
        return Result.success(articleByUUid);
    }

    @ApiOperation("统计所有文章数量")
    @GetMapping("/countArticles")
    public Result countArticles() {
        return Result.success(articleService.countArticles());
    }

    @ApiOperation("统计模糊查询的文章数量")
    @PostMapping("/countConditionArticles")
    public Result countConditionArticles(@RequestBody PageDto pageDto) {
        return Result.success(articleService.countConditionArticles(pageDto));
    }

    @ApiOperation("统计已举报的文章数量")
    @GetMapping("/countReportArticles")
    public Result countReportArticles() {
        return Result.success(articleService.countReportArticle());
    }

    @ApiOperation("统计分类的文章数量")
    @GetMapping("/countArticlesByCategory")
    public Result countArticlesByCategory() {
        return Result.success(articleService.countArticleByCategory());
    }

    @ApiOperation("统计用户的文章数量")
    @GetMapping("/countArticlesByUser")
    public Result countArticlesByUser() {
        return Result.success(articleService.countArticleByUser());
    }

    @ApiOperation("模糊查询已举报的文章数量")
    @PostMapping("/countReportArticlesByCondition")
    public Result countReportArticlesByCondition(@RequestBody PageDto pageDto) {
        return Result.success(articleService.countConditionReportArticles(pageDto));
    }

    @ApiOperation("模糊查询已举报的文章")
    @PostMapping("/findReportArticlesByCondition")
    public Result findReportArticlesByCondition(@RequestBody PageDto pageDto) {
        return Result.success(articleService.findReportArticleByCondition(pageDto));
    }
}
